﻿using MDPM.EntitiyModels.BusinessOrganization;
using MDPM.EntitiyModels.BusinessTenants;
using MDPM.EntitiyModels.CommodityBusiness.HospitalDepartment;
using MDPM.EntitiyModels.Common;
using MDPM.EntitiyModels.MedicalDeviceManagement;
using MDPM.EntitiyModels.ProjectManage.FinanceRelevant;
using MDPM.EntitiyModels.ProjectManage.ProjectRelevant;
using MDPM.EntitiyModels.ProjectManage.PurchaseGroupRelevant;
using MDPM.EntitiyModels.ProjectManage.PurchasingLeadingGroupRelevant;
using MDPM.EntitiyModels.ProjectManage.WorkLogRelevant;
using MDPM.EntitiyModels.UserAndRoles;
using MDPM.ORM;
using Microsoft.International.Converters.PinYinConverter;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MDPM.DataAccess.Seed
{
    public static class ApplicationDataSeed
    {
        static MDPMDbContext _entitiesContext;
        private static string EmailCreater
        {
            get
            {
                Random random = new Random();
                var result = "";
                for (var i = 0; i < 11; i++)
                {
                    result += random.Next(9);
                }
                return result + "@163.com";
            }
        }
        private static string PhoneNumberCreater
        {
            get
            {
                Random random = new Random();
                var result = "134";
                for (var i = 0; i < 8; i++)
                {
                    result += random.Next(9);
                }
                return result;
            }
        }
        
        private static string WebChatCreater
        {
            get
            {
                Random random = new Random();
                var result = "134";
                for (var i = 0; i < 8; i++)
                {
                    result += random.Next(9);
                }
                return result;
            }
        }
        private static string QQCreater
        {
            get
            {
                Random random = new Random();
                var result = "2";
                for (var i = 0; i < 8; i++)
                {
                    result += random.Next(9);
                }
                return result;
            }
        }
        
        private static string WorkNumberCreater
        {
            get
            {
                Random random = new Random();
                var result = "9";
                for (var i = 0; i < 8; i++)
                {
                    result += random.Next(9);
                }
                return result;
            }
        }
        /// <summary>
        /// 汉字转拼音
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public static string PinYin(string name)
        {
            string str = name;
            string result = string.Empty;
            foreach (char item in str)
            {
                try
                {
                    ChineseChar cc = new ChineseChar(item);
                    if (cc.Pinyins.Count > 0 && cc.Pinyins[0].Length > 0)
                    {
                        string temp = cc.Pinyins[0].ToString();
                        result += temp.Substring(0, temp.Length - 1);
                    }
                }
                catch (Exception)
                {
                    result += item.ToString();
                }
            }
            return result;

        }

        public static void InitiaEntity(MDPMDbContext dbContext)
        {
            _entitiesContext = dbContext;
            UserEntities();
        }
        static List<string> departments = new List<string>()
        {
            "诊疗部","辅助诊疗部","护理部","行政后勤部"
        };
        static List<string> departments_1 = new List<string>()
        {
            "内科","外科","妇产科","儿科","药剂科","放射科","临床检验科","病理科","医务科","人事科","财务科","科教科"
        };
        static List<string> surname = new List<string>()
        {
          "赵 ","钱","孙","李","周","吴","郑","王","刘","韦"
        };
        static List<string> nameshu = new List<string>()
        {
            "一 ","二","三","四","五","六","七","八","九","十"
        };


        public static void UserEntities()
        {

            List<string> names = new List<string>();
            for(int i = 0; i < 10; i++)
            {
                for(int j = 0; j < 10; j++)
                {
                    names.Add(surname[i] + nameshu[j]);
                }
            }
            

            #region 住址
            List<CommonAddress> listCommonAddress = new List<CommonAddress>();
            CommonAddress commonAddress1 = new CommonAddress()
            {
                City = "曲靖",
                Province = "云南",
                PostCode = "540000",
                Id = Guid.NewGuid(),
                Name = "中国",
                Region = "七零区",
                Street = "鹧鸪江路8号",
            };

            listCommonAddress.Add(commonAddress1);
            if (!_entitiesContext.Tenants.Any())
            {
                _entitiesContext.CommonAddresss.AddRange(listCommonAddress);
                _entitiesContext.SaveChanges();
            }

            #endregion


            #region 租户
            List<Tenant> listTenant = new List<Tenant>();
                var shangHeTenant = new Tenant()
                {
                    Name = "上和医院",
                    OrganizationCode = "91510104MA6CC78P90",
                    ExpirationTime = new DateTime(2034, 12, 12),
                    TenantType = 1, //代表医院
                    RegisterFund = 5800.00M,
                    Corporate = "谢名洋",
                    CorporateEmail = EmailCreater,
                    CorporatePhone = PhoneNumberCreater,
                    CommonAddress = new CommonAddress()
                    {
                        City = "曲靖",
                        Province = "云南",
                        PostCode = "540000",
                        Id = Guid.NewGuid(),
                        Name = "中国",
                        Region = "七零区",
                        Street = "鹧鸪江路8号",
                    },

                };
                listTenant.Add(shangHeTenant);
            if (!_entitiesContext.Tenants.Any())
            {
                _entitiesContext.Tenants.AddRange(listTenant);
                _entitiesContext.SaveChanges();
            }

            #endregion


            #region 系统账号
            List<AppUser> listAppUser = new List<AppUser>();
            AppUser appUser0 = new AppUser()
            {
                Account = "555",
                PassWord = "555",
                UserName = "555",
                AccountStatus = true,
            };
            listAppUser.Add(appUser0);
            AppUser appUser1 = new AppUser()
            {
                Account = "17687161519liyuanzhang",
                PassWord = "555",
                UserName = "李园长",
                AccountStatus = true,
            };
            listAppUser.Add(appUser1);
            //诊疗部 部长 张霄
            AppUser appUser1_1 = new AppUser()
            {
                Account = "17687161519_zhangxiao",
                PassWord = "555",
                UserName = "张霄",
                AccountStatus = true,
            };
            listAppUser.Add(appUser1_1);

            if (!_entitiesContext.AppUsers.Any())
            {
                _entitiesContext.AppUsers.AddRange(listAppUser);
                _entitiesContext.SaveChanges();
            }
            #endregion

            #region 角色
            List<AppRole> listAppRole = new List<AppRole>();
            AppRole appAppRole0 = new AppRole()
            {
                Tenant = shangHeTenant,
                RoleName = "超级用户",
                PermissionsDescription = "可以管理上和医院的的一切事物",
                Parent = null,

            };
            listAppRole.Add(appAppRole0);
            AppRole appAppRole1 = new AppRole()
            {
                Tenant = shangHeTenant,
                RoleName = "院长",
                PermissionsDescription = "比超级用户小一点",
                Parent = null,

            };
            listAppRole.Add(appAppRole1);
            AppRole appAppRole1_1 = new AppRole()
            {
                Tenant = shangHeTenant,
                RoleName = "部长职位",
                PermissionsDescription = "管理部的",
                Parent = appAppRole1,

            };
            listAppRole.Add(appAppRole1_1);
            AppRole appAppRole1_1_1 = new AppRole()
            {
                Tenant = shangHeTenant,
                RoleName = "科长职位",
                PermissionsDescription = "管理科的",
                Parent = appAppRole1_1,

            };
            listAppRole.Add(appAppRole1_1_1);
            AppRole appAppRole1_1_1_1 = new AppRole()
            {
                Tenant = shangHeTenant,
                RoleName = "普通员工",
                PermissionsDescription = "普通员工",
                Parent = appAppRole1_1_1,

            };
            listAppRole.Add(appAppRole1_1_1_1);
            if (!_entitiesContext.AppRoles.Any())
            {
                _entitiesContext.AppRoles.AddRange(listAppRole);
                _entitiesContext.SaveChanges();
            }
            #endregion

            #region  创建100个账号
            for(int i =0; i < 100; i++)
            {
                AppUser appUser = new AppUser()
                {
                    Account = "17687161519_" + PinYin(names[i]) ,
                    PassWord = "555",
                    UserName = names[i],
                    AccountStatus = true,
                };
                _entitiesContext.AppUsers.Add(appUser);
                if(i < 4)
                {
                    AppUseWithAppRole appUseWithAppRole = new AppUseWithAppRole()
                    {
                        AppRole = _entitiesContext.AppRoles.Where(x => x.RoleName == "部长职位").FirstOrDefault(),
                        AppUser = appUser,
                    };
                    _entitiesContext.AppUseWithAppRoles.Add(appUseWithAppRole);
                }
                if(i>=4 && i < 16)
                {
                    AppUseWithAppRole appUseWithAppRole = new AppUseWithAppRole()
                    {
                        AppRole = _entitiesContext.AppRoles.Where(x => x.RoleName == "科长职位").FirstOrDefault(),
                        AppUser = appUser,
                    };
                    _entitiesContext.AppUseWithAppRoles.Add(appUseWithAppRole);
                }
                else
                {
                    AppUseWithAppRole appUseWithAppRole = new AppUseWithAppRole()
                    {
                        AppRole = _entitiesContext.AppRoles.Where(x => x.RoleName == "普通员工").FirstOrDefault(),
                        AppUser = appUser,
                    };
                    _entitiesContext.AppUseWithAppRoles.Add(appUseWithAppRole);
                }
                _entitiesContext.SaveChanges();

            }
            #endregion


            #region 角色与账号的多对多关系类
            List<AppUseWithAppRole> listAppUseWithAppRoles = new List<AppUseWithAppRole>();
            ///让 555  成为上和医院的超级管理员
            AppUseWithAppRole appUseWithAppRole0 = new AppUseWithAppRole()
            {
                AppRole = appAppRole0,
                AppUser = appUser0,

            };
            listAppUseWithAppRoles.Add(appUseWithAppRole0);
            // 李园长 院长
            AppUseWithAppRole appUseWithAppRole1 = new AppUseWithAppRole()
            {
                AppRole = appAppRole1,
                AppUser = appUser1,

            };
            listAppUseWithAppRoles.Add(appUseWithAppRole1);
            //张霄  诊疗部部长
            AppUseWithAppRole appUseWithAppRole1_1 = new AppUseWithAppRole()
            {
                AppRole = appAppRole1_1,
                AppUser = appUser1_1,

            };
            listAppUseWithAppRoles.Add(appUseWithAppRole1_1);

            if (!_entitiesContext.AppUseWithAppRoles.Any())
            {
                _entitiesContext.AppUseWithAppRoles.AddRange(listAppUseWithAppRoles);
                _entitiesContext.SaveChanges();
            }
            #endregion

            #region 租户组织
            List<Organization> listOrganization = new List<Organization>();
            var shangOrganization = new Organization()
            {
                    Tenant = shangHeTenant,

            };

            if (!_entitiesContext.Organizations.Any())
            {
                _entitiesContext.Organizations.AddRange(listOrganization);
                _entitiesContext.SaveChanges();
            }
            #endregion
            for (int i = 0; i < 4; i++)
            {
                ///诊疗部
                OrganizationDepartment organizationDepartment = new OrganizationDepartment()
                {
                    ResponseEmployee = names[i],
                    Name = departments[i],
                    Organization = shangOrganization,
                    Parent = null,
                };
                _entitiesContext.OrganizationDepartments.Add(organizationDepartment);
                _entitiesContext.SaveChanges();
            }
            for (int i = 0; i < 12; i++)
            {
                if(i < 4)
                {
                    ///诊疗部
                    OrganizationDepartment organizationDepartment = new OrganizationDepartment()
                    {
                        ResponseEmployee = names[i + 4],
                        Name = departments_1[i],
                        Organization = shangOrganization,
                        Parent = _entitiesContext.OrganizationDepartments.Where(x => x.Name == departments[0]).FirstOrDefault(),
                    };
                    _entitiesContext.OrganizationDepartments.Add(organizationDepartment);
                    _entitiesContext.SaveChanges();
                }
                if (i >=4 && i <8)
                {
                    ///诊疗部
                    OrganizationDepartment organizationDepartment = new OrganizationDepartment()
                    {
                        ResponseEmployee = names[i + 4],
                        Name = departments_1[i],
                        Organization = shangOrganization,
                        Parent = _entitiesContext.OrganizationDepartments.Where(x => x.Name == departments[1]).FirstOrDefault(),
                    };
                    _entitiesContext.OrganizationDepartments.Add(organizationDepartment);
                    _entitiesContext.SaveChanges();
                }
                if (i >= 8 && i < 12)
                {
                    ///诊疗部
                    OrganizationDepartment organizationDepartment = new OrganizationDepartment()
                    {
                        ResponseEmployee = names[i + 4],
                        Name = departments_1[i],
                        Organization = shangOrganization,
                        Parent = _entitiesContext.OrganizationDepartments.Where(x => x.Name == departments[3]).FirstOrDefault(),
                    };
                    _entitiesContext.OrganizationDepartments.Add(organizationDepartment);
                    _entitiesContext.SaveChanges();

                }


            }



            #region 租户部门
            List<OrganizationDepartment> listOrganizationDepartment = new List<OrganizationDepartment>();
            ///诊疗部
            OrganizationDepartment organizationDepartment1_1 = new OrganizationDepartment()
            {
                ResponseEmployee="张霄",
                Name = "疗诊部",
                Organization = shangOrganization,
                Id = Guid.NewGuid(),
                Parent = null,

            };
            listOrganizationDepartment.Add(organizationDepartment1_1);
            //疗诊部的骨科
            OrganizationDepartment organizationDepartment1_1_1 = new OrganizationDepartment()
            {
                ResponseEmployee = "胡约",
                Name = "儿科",
                Organization = shangOrganization,
                Id = Guid.NewGuid(),
                Parent = organizationDepartment1_1,

            };
            listOrganizationDepartment.Add(organizationDepartment1_1_1);

            if (!_entitiesContext.OrganizationDepartments.Any())
            {
                _entitiesContext.OrganizationDepartments.AddRange(listOrganizationDepartment);
                _entitiesContext.SaveChanges();
            }
            #endregion


            #region 租户人员
            List<OrganizationDepartmentEmployee> listOrganizationDepartmentEmployee = new List<OrganizationDepartmentEmployee>();
            //555超级管理员
            OrganizationDepartmentEmployee organizationDepartmentEmployee0 = new OrganizationDepartmentEmployee()
            {
                Name = "555",
                IdentificationNumber = "330781198509077158",
                Birthday = new DateTime(1998,11,16),
                Sex = false,
                PhoneNumber = PhoneNumberCreater,
                Email = EmailCreater,
                WebChat = WebChatCreater,
                QQ = QQCreater,
                AppUser = appUser0,
                CommonAddress = commonAddress1,
                OrganizationDepartment = null,
                LaborContractExpirationTime = new DateTime(2039,11,15),
                EmergencyContact ="乔",
                EmergencyContactPhone = "555",
                WorkNumber = WorkNumberCreater,
            };
            listOrganizationDepartmentEmployee.Add(organizationDepartmentEmployee0);
            //人员李园长
            OrganizationDepartmentEmployee organizationDepartmentEmployee1 = new OrganizationDepartmentEmployee()
            {
                Name = "李园长",
                IdentificationNumber = "330781198509072234",
                Birthday = new DateTime(1998, 11, 16),
                Sex = false,
                PhoneNumber = PhoneNumberCreater,
                Email = EmailCreater,
                WebChat = WebChatCreater,
                QQ = QQCreater,
                AppUser = appUser0,
                CommonAddress = commonAddress1,
                OrganizationDepartment = null,
                LaborContractExpirationTime = new DateTime(2039, 11, 15),
                EmergencyContact = "不止",
                EmergencyContactPhone = "8693576",
                WorkNumber = WorkNumberCreater,
            };
            listOrganizationDepartmentEmployee.Add(organizationDepartmentEmployee1);
            //人员张霄部长
            OrganizationDepartmentEmployee organizationDepartmentEmployee1_1 = new OrganizationDepartmentEmployee()
            {
                Name = "张霄",
                IdentificationNumber = "330781198509077270",
                Birthday = new DateTime(1995, 11, 16),
                Sex = false,
                PhoneNumber = PhoneNumberCreater,
                Email = EmailCreater,
                WebChat = WebChatCreater,
                QQ = QQCreater,
                AppUser = appUser1_1,
                CommonAddress = commonAddress1,
                OrganizationDepartment = organizationDepartment1_1,
                LaborContractExpirationTime = new DateTime(2039, 11, 15),
                EmergencyContact = "不知道啊",
                EmergencyContactPhone = "8693576",
                WorkNumber = WorkNumberCreater,
            };
            listOrganizationDepartmentEmployee.Add(organizationDepartmentEmployee1_1);

            if (!_entitiesContext.OrganizationDepartmentEmployees.Any())
            {
                _entitiesContext.OrganizationDepartmentEmployees.AddRange(listOrganizationDepartmentEmployee);
                _entitiesContext.SaveChanges();
            }
            #endregion

            #region 加入100个人
            for(int i = 0; i < 100; i++)
            {
                OrganizationDepartmentEmployee organizationDepartmentEmployee = new OrganizationDepartmentEmployee()
                {
                    Name = names[i],
                    IdentificationNumber = "330781198509077158",
                    Birthday = new DateTime(1998, 11, 16),
                    Sex = false,
                    PhoneNumber = PhoneNumberCreater,
                    Email = EmailCreater,
                    WebChat = WebChatCreater,
                    QQ = QQCreater,
                    AppUser = _entitiesContext.AppUsers.Where(x => x.UserName == names[i]).FirstOrDefault(),
                    CommonAddress = commonAddress1,
                    OrganizationDepartment = null,
                    LaborContractExpirationTime = new DateTime(2039, 11, 15),
                    EmergencyContact = "乔",
                    EmergencyContactPhone = "555",
                    WorkNumber = WorkNumberCreater,
                };
                _entitiesContext.OrganizationDepartmentEmployees.Add(organizationDepartmentEmployee);
                OrganizationDepartmentWithOrganizationDepartmentEmployee departmentWithEmployee = new OrganizationDepartmentWithOrganizationDepartmentEmployee()
                {
                    OrganizationDepartmentEmployee = organizationDepartmentEmployee,
                    OrganizationDepartment = _entitiesContext.OrganizationDepartments.Where(x => x.ResponseEmployee == names[i]).FirstOrDefault(),
                };
                if(departmentWithEmployee.OrganizationDepartment == null)
                {
                    departmentWithEmployee.OrganizationDepartment = _entitiesContext.OrganizationDepartments.Where(x => x.Name == departments_1[RandomNumber(0, 11)[0]]).FirstOrDefault();
                }
                _entitiesContext.OrganizationDepartmentWithOrganizationDepartmentEmployees.Add(departmentWithEmployee);
                _entitiesContext.SaveChanges();

            }
            #endregion

            #region 部门与人员的多对多关系
            List<OrganizationDepartmentWithOrganizationDepartmentEmployee> listOrganizationDepartmentWithOrganizationDepartmentEmployee = new List<OrganizationDepartmentWithOrganizationDepartmentEmployee>();
            //张霄进入疗诊部
            OrganizationDepartmentWithOrganizationDepartmentEmployee departmentWithEmployee_zhangxiao_liaozheng = new OrganizationDepartmentWithOrganizationDepartmentEmployee()
            {
                OrganizationDepartmentEmployee = organizationDepartmentEmployee1_1,
                OrganizationDepartment = organizationDepartment1_1,
            };
            listOrganizationDepartmentWithOrganizationDepartmentEmployee.Add(departmentWithEmployee_zhangxiao_liaozheng);

            if (!_entitiesContext.OrganizationDepartmentWithOrganizationDepartmentEmployees.Any())
            {
                _entitiesContext.OrganizationDepartmentWithOrganizationDepartmentEmployees.AddRange(listOrganizationDepartmentWithOrganizationDepartmentEmployee);
                _entitiesContext.SaveChanges();
            }
            #endregion

            #region 采购领导小组成员
            List<PurchasingLeadingGroupMeber> listPurchasingLeadingGroupMeber = new List<PurchasingLeadingGroupMeber>();
            PurchasingLeadingGroupMeber purchasingLeadingGroupMeber1 = new PurchasingLeadingGroupMeber
            {
                Position = true,
                Member = _entitiesContext.OrganizationDepartmentEmployees.Where(x => x.Name == "张三").FirstOrDefault(),
            };
            listPurchasingLeadingGroupMeber.Add(purchasingLeadingGroupMeber1);
            PurchasingLeadingGroupMeber purchasingLeadingGroupMeber2 = new PurchasingLeadingGroupMeber
            {
                Position = false,
                Member = _entitiesContext.OrganizationDepartmentEmployees.Where(x => x.Name == "李四").FirstOrDefault(),
            };
            listPurchasingLeadingGroupMeber.Add(purchasingLeadingGroupMeber2);

            if (!_entitiesContext.PurchasingLeadingGroupMebers.Any())
            {
                _entitiesContext.PurchasingLeadingGroupMebers.AddRange(listPurchasingLeadingGroupMeber);
                _entitiesContext.SaveChanges();
            }
            #endregion

            #region 部门需求
            List<HospitalDepartmentDemand> listHospitalDepartmentDemand = new List<HospitalDepartmentDemand>();
            ///诊疗部提出小儿吸痰器需求
            HospitalDepartmentDemand hospitalDepartmentDemand_xitanqi = new HospitalDepartmentDemand
            {
                HospitalDepartment = organizationDepartment1_1,
                DeclareReason = "我们缺少此机器",
                ApprovalRemarks = "同意",
                ApprovalPerson = _entitiesContext.PurchasingLeadingGroupMebers.Where(x => x.Member.Name == "张三").FirstOrDefault(),
                DemandDate = new DateTime(2021,12,19),
                ApprovalCompletedTime = new DateTime(2021,12,29),
                IsPass = true,

            };
            listHospitalDepartmentDemand.Add(hospitalDepartmentDemand_xitanqi);

            if (!_entitiesContext.HospitalDepartmentDemands.Any())
            {
                _entitiesContext.HospitalDepartmentDemands.AddRange(listHospitalDepartmentDemand);
                _entitiesContext.SaveChanges();
            }
            #endregion

            

            #region 项目相关
            //项目
            List<Project> listProject = new List<Project>();
            Project project1 = new Project
            {
                ProjectStatusEnum = ProjectStatusEnum.新建,
                ProjectDemand = hospitalDepartmentDemand_xitanqi,
                ProjectStartTime = DateTime.Now
            };
            listProject.Add(project1);

            if (!_entitiesContext.Projects.Any())
            {
                _entitiesContext.Projects.AddRange(listProject);
                _entitiesContext.SaveChanges();
            }
            //项目采购小组
            List<PurchaseGroup> listPurchaseGroup = new List<PurchaseGroup>();
            PurchaseGroup purchaseGroup1 = new PurchaseGroup
            {
                Name = "儿科化痰器采购小组",
                CreationTime = DateTime.Now,
                NumberForPerson = 2,
                GroupProject = project1,
                GroupLeader = _entitiesContext.OrganizationDepartmentEmployees.Where(x => x.Name=="王五").FirstOrDefault(),
            };
            listPurchaseGroup.Add(purchaseGroup1);

            if (!_entitiesContext.PurchaseGroups.Any())
            {
                _entitiesContext.PurchaseGroups.AddRange(listPurchaseGroup);
                _entitiesContext.SaveChanges();
            }
            //项目采购小组成员
            List<PurchaseGroupMember> listPurchaseGroupMember = new List<PurchaseGroupMember>();
            PurchaseGroupMember purchaseGroupMember1 = new PurchaseGroupMember
            {
              Position =true,
                GroupMember = _entitiesContext.OrganizationDepartmentEmployees.Where(x => x.Name == "王五").FirstOrDefault(),
                PurchaseGroup = purchaseGroup1,
            };
            listPurchaseGroupMember.Add(purchaseGroupMember1);
            PurchaseGroupMember purchaseGroupMember2 = new PurchaseGroupMember
            {
                Position = true,
                GroupMember = _entitiesContext.OrganizationDepartmentEmployees.Where(x => x.Name == "王六").FirstOrDefault(),
                PurchaseGroup = purchaseGroup1,
            };
            listPurchaseGroupMember.Add(purchaseGroupMember2);

            if (!_entitiesContext.PurchaseGroupMembers.Any())
            {
                _entitiesContext.PurchaseGroupMembers.AddRange(listPurchaseGroupMember);
                _entitiesContext.SaveChanges();
            }

            #endregion

            #region 项目里程碑
            //项目里程碑
            List<ProjectMileStone> listProjectMileStone = new List<ProjectMileStone>();
            ProjectMileStone projectMileStone1 = new ProjectMileStone
            {
                Name = "项目新建",
                StartDate = DateTime.Now,
                EndDate = new DateTime(2033,2,3),
                ParentMileStone = null,
                Project = project1,

            };
            listProjectMileStone.Add(projectMileStone1);

            if (!_entitiesContext.ProjectMileStones.Any())
            {
                _entitiesContext.ProjectMileStones.AddRange(listProjectMileStone);
                _entitiesContext.SaveChanges();
            }

            #endregion

            #region 项目工作
            //项目工作
            List<ProjectWork> listProjectWork = new List<ProjectWork>();
            ProjectWork projectWork1 = new ProjectWork
            {
                Name = "制定初始计划工作",
                Description = "项目刚开始我们要制定一些初始计划",
                MileStone = projectMileStone1,


            };
            listProjectWork.Add(projectWork1);

            if (!_entitiesContext.ProjectWorks.Any())
            {
                _entitiesContext.ProjectWorks.AddRange(listProjectWork);
                _entitiesContext.SaveChanges();
            }

            #endregion

            #region 项目任务
            //项目任务
            List<ProjectTask> listProjectTask = new List<ProjectTask>();
            ProjectTask projectTask1 = new ProjectTask
            {
                Name = "查阅资料任务",
                ProjectTaskTypeEnum = ProjectTaskTypeEnum.定义,
                Description = "查阅器材相关资料",
                StartDate = DateTime.Now,
                EndDate = new DateTime(2033, 2, 3),
                User = _entitiesContext.PurchaseGroupMembers.Where(x => x.GroupMember.Name == "王六").FirstOrDefault(),
                Work = projectWork1,
            };
            listProjectTask.Add(projectTask1);

            if (!_entitiesContext.ProjectTasks.Any())
            {
                _entitiesContext.ProjectTasks.AddRange(listProjectTask);
                _entitiesContext.SaveChanges();
            }

            #endregion

            #region 任务障碍
            //任务障碍
            List<ProjectObstacle> listProjectObstacle = new List<ProjectObstacle>();
            ProjectObstacle projectObstacle1 = new ProjectObstacle
            {
                Name = "查阅资料遇到了问题 ",
                Description = "网络不好",
                StartDate = DateTime.Now,
                EndDate = new DateTime (2033,4,4),
                Solution = "建议更换网络",
                ProjectTask = projectTask1,
                SubmitPerson = _entitiesContext.PurchaseGroupMembers.Where(x => x.GroupMember.Name == "王六").FirstOrDefault(),
            };
            listProjectObstacle.Add(projectObstacle1);

            if (!_entitiesContext.ProjectObstacles.Any())
            {
                _entitiesContext.ProjectObstacles.AddRange(listProjectObstacle);
                _entitiesContext.SaveChanges();
            }

            #endregion

            #region 项目财务
            //项目财务
            List<Finance> listFinance = new List<Finance>();
            Finance finance1 = new Finance
            {
                Remark = " 五憋住",
                PlanFunding = 12M,
                ActualFunding = 15M,
                Project = project1,


            };
            listFinance.Add(finance1);

            if (!_entitiesContext.Finances.Any())
            {
                _entitiesContext.Finances.AddRange(listFinance);
                _entitiesContext.SaveChanges();
            }

            #endregion

            #region 财务条目
            //财务条目
            List<FinanceItem> listFinanceItem = new List<FinanceItem>();
            FinanceItem financeItem1 = new FinanceItem
            {
                Remark = " 财务条目备注",
                PlanFunding = 3M,
                ActualFunding = 1M,
                ProjectTask = projectTask1,
                Finance = finance1,



            };
            listFinanceItem.Add(financeItem1);

            if (!_entitiesContext.FinanceItems.Any())
            {
                _entitiesContext.FinanceItems.AddRange(listFinanceItem);
                _entitiesContext.SaveChanges();
            }

            #endregion

            #region 工作日志
            //工作日志
            List<WorkLog> listWorkLog = new List<WorkLog>();
            WorkLog workLog1 = new WorkLog
            {
                Creator = purchaseGroupMember2,
                CreateTime = DateTime.Now,
                LogContent = "日志内容",
                Project = project1,

            };
            listWorkLog.Add(workLog1);

            if (!_entitiesContext.WorkLogs.Any())
            {
                _entitiesContext.WorkLogs.AddRange(listWorkLog);
                _entitiesContext.SaveChanges();
            }

            #endregion

            #region 医疗器械
            //医疗器械
            List<MedicalDevice> listMedicalDevice = new List<MedicalDevice>();
            MedicalDevice medicalDevice1 = new MedicalDevice
            {
                Name = "X光机器",
                Owner = shangHeTenant,
                PurchasecCost = 13M,
                Vendor = null,
                ServiceTime = DateTime.Now,
                Brand = "大海",
                PutIntoUseTime = DateTime.Now,

            };
            listMedicalDevice.Add(medicalDevice1);

            if (!_entitiesContext.MedicalDevices.Any())
            {
                _entitiesContext.MedicalDevices.AddRange(listMedicalDevice);
                _entitiesContext.SaveChanges();
            }

            #endregion

            #region 医疗器械维修记录
            //医疗器械维修记录
            List<MaintenanceRecords> listMaintenanceRecords = new List<MaintenanceRecords>();
            MaintenanceRecords maintenanceRecords1 = new MaintenanceRecords
            {
                DownTime = DateTime.Now,
                DownReason = "不知道诶",
                RepairNotes = "以后注意不要踢他",
                RepairTime = DateTime.Now,
                MedicalDevice = medicalDevice1,

            };
            listMaintenanceRecords.Add(maintenanceRecords1);

            if (!_entitiesContext.MaintenanceRecordss.Any())
            {
                _entitiesContext.MaintenanceRecordss.AddRange(listMaintenanceRecords);
                _entitiesContext.SaveChanges();
            }

            #endregion


            //if (!_entitiesContext.Users.Any())
            //{
            //    var roler = new Roles()
            //    {
            //        no = Guid.NewGuid(),
            //        Name = "jaja"
            //    };
            //    _entitiesContext.Roless.Add(roler);

            //    var user = new AppUser()
            //    {
            //        Id = Guid.NewGuid(),
            //        name = "dkj",
            //        Roles = roler
            //    };
            //    _entitiesContext.Users.Add(user);
            //    _entitiesContext.SaveChanges();
            //}
        }
        /// <summary>
        /// 创建一个得到随机数数组的方法(方法参数代表随机数范围,生成的数组中含benin,end）
        /// </summary>
        private static List<Int32> RandomNumber(int begin, int end)
        {
            List<Int32> sjList = new List<Int32>();
            for (int i = 0; i < 100; i++)
            {

                byte[] buffer = Guid.NewGuid().ToByteArray();
                int iSeed = BitConverter.ToInt32(buffer, 0);
                Random random = new Random(iSeed);
                sjList.Add(random.Next(begin, end + 1));
            }
            //去除数组重复值
            List<Int32> nlist = sjList;
            for (int i = 0; i < sjList.Count; i++)
            {

                for (int j = sjList.Count - 1; j > i; j--)
                {
                    if (sjList[i] == nlist[j])
                    {
                        sjList.RemoveAt(j);
                    }
                }

            }
            return sjList;
        }
    }
}
